package org.keyroy.tag;

import java.lang.reflect.Field;
import java.util.Hashtable;

public class SimpleXmlObject extends XmlObject {

	@Override
	public final void initObj(Tag tag) {
		Field[] fields = getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			try {
				Class<?> fClass = fields[i].getType();
				Parser parser = baseClassTable.get(fClass);
				if (parser != null) {
					fields[i].set(this, parser.parser(tag.getParameterValue(fields[i].getName())));
				} else if (XmlSupport.class.isAssignableFrom(fClass)) {
					XmlSupport object = ((XmlSupport) (fClass.newInstance()));
					Tag cTag = tag.getChildTag(object.getTagName());
					if (cTag != null) {
						object.initObj(tag);
					}
					fields[i].set(this, object);
				}
			} catch (Exception e) {
			}
		}
	}

	@Override
	public final void initTag(Tag tag) {
		Field[] fields = getClass().getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
			try {
				Class<?> fClass = fields[i].getType();
				Parser parser = baseClassTable.get(fClass);
				if (parser != null) {
					tag.addParameter(fields[i].getName(), String.valueOf(fields[i].get(this)));
				} else if (XmlSupport.class.isAssignableFrom(fClass)) {
					XmlSupport object = (XmlSupport) fields[i].get(this);
					tag.addChild(object);
				}
			} catch (Exception e) {
			}
		}
	}

	private static Hashtable<Class<?>, Parser> baseClassTable = new Hashtable<Class<?>, Parser>();
	static {
		baseClassTable.put(int.class, new Parser() {
			@Override
			public Object parser(String s) {
				return Integer.parseInt(s);
			}
		});
		baseClassTable.put(long.class, new Parser() {
			@Override
			public Object parser(String s) {
				return Long.parseLong(s);
			}
		});
		baseClassTable.put(float.class, new Parser() {
			@Override
			public Object parser(String s) {
				return Float.parseFloat(s);
			}
		});
		baseClassTable.put(double.class, new Parser() {
			@Override
			public Object parser(String s) {
				return Double.parseDouble(s);
			}
		});
		baseClassTable.put(boolean.class, new Parser() {

			@Override
			public Object parser(String s) {
				return Boolean.parseBoolean(s);
			}
		});

		baseClassTable.put(Integer.class, new Parser() {
			@Override
			public Object parser(String s) {
				return Integer.parseInt(s);
			}
		});
		baseClassTable.put(Long.class, new Parser() {
			@Override
			public Object parser(String s) {
				return Long.parseLong(s);
			}
		});
		baseClassTable.put(Float.class, new Parser() {
			@Override
			public Object parser(String s) {
				return Float.parseFloat(s);
			}
		});
		baseClassTable.put(Double.class, new Parser() {
			@Override
			public Object parser(String s) {
				return Double.parseDouble(s);
			}
		});
		baseClassTable.put(Boolean.class, new Parser() {
			@Override
			public Object parser(String s) {
				return Boolean.parseBoolean(s);
			}
		});

		baseClassTable.put(String.class, new Parser() {
			@Override
			public Object parser(String s) {
				return s;
			}
		});
	}

	private interface Parser {
		public Object parser(String s);
	}
}
